<?php

defined('BASEPATH') or die('forbidden');

class Index_Controller Extends Controller {

    function index() {
        if (stripos($_SERVER["HTTP_USER_AGENT"], 'MSIE') !== FALSE) {
            //show_message("{$this->config['sitename']}后台管理不支持使用IE核心的浏览器,请使用基于Webkit核心(Firefox、Chrome、Safari等)的浏览器重试", "javascript:;", 0, 'help');
        }
        check_level(50);
        $data = array();
        $areadata = array();
        $config = get_config();
        $this->wholeServerHost = $config['host_url'];
        //选择服务器
        //$areadata[] = array('id' => $this->config['area_id'], 'name' => $this->config['area_name'], 'admin' => base_url(), 'state' => 'normal');
        //$this->db->query("SELECT * FROM {pre}areasample  ORDER BY weigh DESC");
        //var_dump($this->db->get_array());
        //获取服务器列表
        $server_list=get_server_list();
        foreach($server_list as $k=>$row) {
        	$row['SvrName']=$row['SvrID']."-".$row['SvrName'];
            $areadata[$row['SvrID']] = $row;
        }
        if (!empty($_REQUEST['area_selected_id'])) {
            $id = $_SESSION['area_selected_id'] = $_REQUEST['area_selected_id'];
            $row=one($id,$server_list);
            //$row = $this->db->one("SELECT * FROM {pre}areasample WHERE id='$id'");

            $_SESSION['area_selected_host'] = $row['SvrOutAddr'];
        }
        //array_unshift($areadata, "未选择服务器");
        $data['areadata'] = $areadata;

        if (!empty($_SESSION['area_selected_id']))
        {
            $area_selected_name = $areadata[$_SESSION['area_selected_id']]['SvrName'];
        }
        else
        {
            $area_selected_name = "未选择服务器";
        }
		
        $this->view->assign("area_selected_name", $area_selected_name);
        $this->view->display("index/index", $data);
    }

    function left() {
        check_level(50);
        $this->view->display("index/left");
    }

    function login() {
        if ($this->auth->check_level(50, null, null, null, true))
            error(translate('INDEX_DO_NOTLOGIN_AGAIN'), site_url("index"), 1000);
        if (is_submit()) {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $url = $this->input->post('url');
            $url = $url == '' ? site_url('index') : $url;
            //$this->cookie->domain = substr($_SERVER['SERVER_NAME'], stripos($_SERVER['SERVER_NAME'], "."));
            if ($this->auth->login($username, $password)) {
                insert_log(translate('HOME'),translate('INDEX_NO_PERMISSION'), array('id'=>$this->auth->user_id));
                if ($this->auth->role_rank >= 50) {
                    $_SESSION['is_admin'] = md5s($this->auth->fields['id'] . $this->auth->fields['sessionid']);
                    $this->cookie->set("is_admin", md5s($this->auth->fields['id'] . $this->auth->fields['sessionid']), 0, base_url());
                    go($url);
                } else {
                    $this->_logout();
                    error(translate('INDEX_NO_PERMISSION'));
                }
            } else {
                insert_log(translate('HOME'),translate('INDEX_NO_PERMISSION'), array('error'=>$this->auth->error));
                $this->_logout();
                error($this->auth->error);
            }
        }
        $this->view->assign("url", $this->input->get("url"));
        $this->view->display("index/login");
    }

    function logout() {
        $this->_logout();
        header("location:" . site_url('index'));
    }

    function _logout() {
        $this->auth->logout(base_url());
        //$this->cookie->domain = substr($_SERVER['SERVER_NAME'], stripos($_SERVER['SERVER_NAME'], "."));
        $this->cookie->clear('user_id', base_url());
        $this->cookie->clear('is_admin', base_url());
        $this->cookie->clear('safecode', base_url());
        $this->cookie->clear('username', base_url());
        $this->cookie->clear('sessionid', base_url());
    }

    function check_area() {

        $areadata = array();
        $this->db->query("SELECT * FROM {pre}areasample WHERE 1 ORDER BY weigh DESC");
        while ($row = $this->db->get_array()) {
            $areadata[$row['id']] = $row;
        }
        $error_area = array();
        foreach ($areadata as $key => $value) {    
            //上线的服务器才请求在线人数
            if ($value['state'] == "normal") {
                $strResult = vpost($value['host'] . "action=9");

                //异常的服务器存入SESSION中
                if ($strResult === FALSE) {
                    $error_area[$value['id']] = $value['name'];
                }
            }
        }
        if (count($error_area) == 0) {
            $str = "<label style='color:#00FF00'>正常</label>";
        } else {
            $str = "<label style='color:#FF0000'>";
            foreach ($error_area as $value) {
                $str .= $value . ",";
            }
            $str = substr($str, 0, -1);
            //$str .=$strResult;
            $str.="</label>";
        }

        echo "<label style='color:#FF0000'>" . $str . "</label>";
    }

}